﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplicationTest
{
    public partial class CheckInfo : System.Web.UI.Page
    {
        String KEY = "";
        String PARAM = "";

        /// <summary>
        /// 获取请求参数信息
        /// </summary>
        private String getParam(String LogName = "")
        {
            String Url = Request.Url.ToString();
            String param = "";
            if (Url.Contains("?"))
            {
                param = Url.Substring(Url.IndexOf("?") + 1);                // 获取参数信息

                if (LogName.Equals("")) LogName = this.GetType().Name;
                LogTool log = new LogTool(LogName);                         // 记录至log中
                log.WriteLine(param);
            }
            return param;
        }

        /// <summary>
        /// 解析主键Key和其他相关参数信息
        /// </summary>
        private String getKey(String queryString)
        {
            int index = queryString.IndexOf("KEY=") + "KEY=".Length;
            String KEY = queryString.Substring(index);
            return KEY;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            String url = "http://" + Request.Params.Get("HTTP_HOST") + "/CheckInfo.aspx?KEY=" + DateTime.Now.Ticks;
            Label1.Text = url;

            String queryString = getParam();      // 获取参数
            if (queryString.Equals("")) return;

            KEY = getKey(queryString);
            if (KEY != null && !KEY.Equals(""))
            {
                if (CheckKeyInfo(KEY)) Response.Write(KEY + "检测通过");
                else
                {
                    SaveInfo(KEY, "NULL");
                    Response.Write(KEY + "检测未通过");
                }
            }
        }

        String TAB = "CheckInfo表";
        String infoURL = "http://localhost:5517/WebInfo.aspx";

        /// <summary>
        /// 检测key是否检测通过
        /// </summary>
        public bool Contains(String key)
        {
            String url = infoURL + "?" + "TAB=" + TAB + "&" + "KEY=" + key;
            String data = getWebData(url);
            return (data.Contains("\"" + key + "\""));
        }

        /// <summary>
        /// 检测key是否检测通过
        /// </summary>
        public bool CheckKeyInfo(String key)
        {
            String url = infoURL + "?"+"TAB=" + TAB + "&" + "KEY=" + key;
            String data = getWebData(url);
            return (data.Contains(key + "-Checked"));
        }

        /// <summary>
        /// 保存信息
        /// </summary>
        public void SaveInfo(String key, String vlaue)
        {
            if (Contains(key)) return;

            String url = infoURL + "?" + "TAB=" + TAB + "&" + "KEY=" + key + "&" + "VALUE=" + vlaue;
            String data = getWebData(url);
        }

        /// <summary>
        /// 修改信息
        /// </summary>
        public void ModifyInfo(String key, String vlaue)
        {
            String url = infoURL + "?" + "TAB=" + TAB + "&" + "KEY=" + key + "&" + "VALUE_M=" + vlaue;
            String data = getWebData(url);
        }

        /// <summary>
        /// 获取指定url的网页数据
        /// </summary>
        public static String getWebData(String url)
        {
            String data = "";

            WebClient client = new WebClient();
            client.Encoding = System.Text.Encoding.UTF8;
            data = client.DownloadString(url);

            return data;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            ModifyInfo(KEY, KEY + "-Checked");
        }

    }
}